package org.yn.car.controller;


import org.yn.car.dto.SaveCarResourceDTO;
import org.yn.car.service.ICarResourcesService;
import org.yn.car.domain.CarResources;
import org.yn.car.query.CarResourcesQuery;
import org.yn.base.util.AjaxResult;
import org.yn.base.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.yn.auth.annotation.PermissionAnnotation;
import java.util.List;

@PermissionAnnotation(name="carResources管理",descs = "carResources管理")
@Api("carResources管理接口")
@RestController
@RequestMapping("/carResources")
public class CarResourcesController {
    @Autowired
    public ICarResourcesService carResourcesService;


    /**
     * 保存和修改公用的
     * @param carResources 传递的实体
     * @return Ajaxresult转换结果
     */
    @PermissionAnnotation(name = "保存和修改CarResources", descs = "保存和修改CarResources")
    @ApiOperation(value = "保存和修改")
    @PostMapping
    public AjaxResult updateOrInsert(@RequestBody CarResources carResources) {
        if ( carResources.getId() != null)
            carResourcesService.update(carResources);
            else
            carResourcesService.insert(carResources);
        return AjaxResult.success();
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @PermissionAnnotation(name = "删除CarResources", descs = "删除CarResources")
    @ApiOperation(value = "删除")
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
            carResourcesService.delete(id);
        return AjaxResult.success();
    }

    //获取用户
    @PermissionAnnotation(name = "获取单个CarResources", descs = "获取单个CarResources")
    @ApiOperation(value = "获取单个CarResources")
    @GetMapping("/{id}")
    public AjaxResult selectById(@PathVariable("id") Long id) {
        CarResources carResources =carResourcesService.selectById(id);
        return AjaxResult.success(carResources);
    }


    /**
    * 查看所有的XX信息
    * @return
    */
    @PermissionAnnotation(name = "获取所有CarResources", descs = "获取所有CarResources")
    @ApiOperation(value = "获取所有CarResources")
    @GetMapping
    public AjaxResult selectAll() {

        List< CarResources> list = carResourcesService.selectAll();
        return AjaxResult.success(list);
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageInfo 分页对象
    */
    @PermissionAnnotation(name = "分页查询CarResources", descs = "分页查询CarResources")
    @ApiOperation(value = "分页查询CarResources")
    @PutMapping
    public AjaxResult queryPage(@RequestBody CarResourcesQuery query) {
        PageInfo<CarResources> pageInfo = carResourcesService.queryPage(query);
        return AjaxResult.success(pageInfo);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PermissionAnnotation(name = "批量删除CarResources", descs = "批量删除CarResources")
    @ApiOperation(value = "批量删除CarResources")
    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Long> ids) {
            carResourcesService.batchDelete(ids);
        return AjaxResult.success();
    }

    /**
     * 批量添加CarResources
     * @param
     * @return
     */
    @PermissionAnnotation(name = "批量添加CarResources", descs = "批量添加CarResources")
    @ApiOperation(value = "批量添加CarResources")
    @PutMapping("/batch")
    public AjaxResult batchAdd(@RequestBody SaveCarResourceDTO dto) {
        carResourcesService.batchSaveCarResources(dto);
        return AjaxResult.success();
    }

    /**
     * 根据car id获取资源图片
     * @param carId
     * @return
     */
    @GetMapping("/carId/{carId}")
    public AjaxResult getCarResourceByCarId(@PathVariable("carId")Long carId){
        List<CarResources> resources = carResourcesService.getCarResourceByCarId(carId);
        return AjaxResult.success(resources);
    }


}